home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Headers / bsd / sys / mbuf.h < prev    next >
C/C++ Source or Header  |  1995-02-14  |  8KB  |  259 lines

  1. /* 
  2.  * Mach Operating System
  3.  * Copyright (c) 1987 Carnegie-Mellon University
  4.  * All rights reserved.  The CMU software License Agreement specifies
  5.  * the terms and conditions for use and redistribution.
  6.  */
  7. /*
  8.  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
  9.  * All rights reserved.
  10.  *
  11.  * Copyright (c) 1994 NeXT Computer, Inc. All rights reserved.
  12.  *
  13.  * Redistribution and use in source and binary forms are permitted
  14.  * provided that this notice is preserved and that due credit is given
  15.  * to the University of California at Berkeley. The name of the University
  16.  * may not be used to endorse or promote products derived from this
  17.  * software without specific prior written permission. This software
  18.  * is provided ``as is'' without express or implied warranty.
  19.  **********************************************************************
  20.  * HISTORY
  21.  * 20-May-94  AOF (NeXT) Stanford MULTICAST
  22.  *
  23.  * 27-Sep-89  Morris Meyer (mmeyer) at NeXT
  24.  *    NFS 4.0 Changes.
  25.  *
  26.  * 11-Aug-87  Peter King (king) at NeXT
  27.  *    SUN_RPC: Added support for "funny" mbuf clusters.  MFREE calls
  28.  *         mclput() rather than MCLFREE.
  29.  *
  30.  * 18-Nov-86  David Golub (dbg) at Carnegie-Mellon University
  31.  *    Remove all uses of 'pte' from machine-independent code.
  32.  *
  33.  *    @(#)mbuf.h    7.8.1.2 (Berkeley) 2/8/88
  34.  */
  35.  
  36. /*
  37.  * Constants related to memory allocator.
  38.  */
  39. #define    MSIZE        128            /* size of an mbuf */
  40.  
  41. /* CLBYTES is not a constant for the NeXT */
  42. #if CLBYTES > 1024 || defined(NeXT)
  43. #define    MCLBYTES    1024
  44. #define    MCLSHIFT    10
  45. #define    MCLOFSET    (MCLBYTES - 1)
  46. #else
  47. #define    MCLBYTES    CLBYTES
  48. #define    MCLSHIFT    CLSHIFT
  49. #define    MCLOFSET    CLOFSET
  50. #endif
  51.  
  52. #define    MMINOFF        12            /* mbuf header length */
  53. #define    MTAIL        4
  54. #define    MMAXOFF        (MSIZE-MTAIL)        /* offset where data ends */
  55. #define    MLEN        (MSIZE-MMINOFF-MTAIL)    /* mbuf data length */
  56. #if defined(GATEWAY) || defined(NeXT)
  57. #define    NMBCLUSTERS    512
  58. #else
  59. #define    NMBCLUSTERS    256
  60. #endif
  61. #define    NMBPCL        (CLBYTES/MSIZE)        /* # mbufs per cluster */
  62.  
  63. /*
  64.  * Macros for type conversion
  65.  */
  66.  
  67. /* network cluster number to virtual address, and back */
  68. #define    cltom(x) ((struct mbuf *)((int)mbutl + ((x) << MCLSHIFT)))
  69. #define    mtocl(x) (((int)x - (int)mbutl) >> MCLSHIFT)
  70.  
  71. /* address in mbuf to mbuf head */
  72. #define    dtom(x)        ((struct mbuf *)((int)x & ~(MSIZE-1)))
  73.  
  74. /* mbuf head, to typed data */
  75. #define    mtod(x,t)    ((t)((int)(x) + (x)->m_off))
  76.  
  77. /*
  78.  * Standard 4.3 supports two kinds of mbufs: normal mbufs, with data
  79.  * residing in the mbuf itself; and cluster mbufs with data stored
  80.  * elsewhere (at a higher address than the corresponding mbuf).
  81.  * The data area for cluster mbufs is allocated as a pool and is
  82.  * managed by the mbuf system itself.  We add another kind of cluster
  83.  * mbuf, whose data area is not part of the mbuf system.  Instead,
  84.  * a routine wishing to use this kind of mbuf must provide a buffer
  85.  * for the storage associated with the mbuf and a pointer to a routine
  86.  * to free the buffer when the mbuf is freed.  The fields in mun_cl
  87.  * below record the necessary bookkeeping information.
  88.  */
  89. struct mbuf {
  90.     struct    mbuf *m_next;        /* next buffer in chain */
  91.     u_long    m_off;            /* offset of data */
  92.     short    m_len;            /* amount of data in this mbuf */
  93.     short    m_type;            /* mbuf type (0 == free) */
  94.     union {
  95.         u_char    mun_dat[MLEN];    /* data storage */
  96.         struct {
  97.             short    mun_cltype;    /* "cluster" type */
  98.             int    (*mun_clfun)();
  99.             int    mun_clarg;
  100.             int    (*mun_clswp)();
  101.         } mun_cl;
  102.     } m_un;
  103.     struct    mbuf *m_act;        /* link in higher-level mbuf list */
  104. };
  105. #define    m_dat    m_un.mun_dat
  106. #define    m_cltype m_un.mun_cl.mun_cltype
  107. #define    m_clfun    m_un.mun_cl.mun_clfun
  108. #define    m_clarg    m_un.mun_cl.mun_clarg
  109. #define    m_clswp    m_un.mun_cl.mun_clswp
  110.  
  111. /* mbuf types */
  112. #define    MT_FREE        0    /* should be on free list */
  113. #define    MT_DATA        1    /* dynamic (data) allocation */
  114. #define    MT_HEADER    2    /* packet header */
  115. #define    MT_SOCKET    3    /* socket structure */
  116. #define    MT_PCB        4    /* protocol control block */
  117. #define    MT_RTABLE    5    /* routing tables */
  118. #define    MT_HTABLE    6    /* IMP host tables */
  119. #define    MT_ATABLE    7    /* address resolution tables */
  120. #define    MT_SONAME    8    /* socket name */
  121. #define    MT_ZOMBIE    9    /* zombie proc status */
  122. #define    MT_SOOPTS    10    /* socket options */
  123. #define    MT_FTABLE    11    /* fragment reassembly header */
  124. #define    MT_RIGHTS    12    /* access rights */
  125. #define    MT_IFADDR    13    /* interface address */
  126. #define MT_IPMOPTS    14    /* internet multicast options */
  127. #define MT_IPMADDR    15    /* internet multicast address */
  128. #define MT_IFMADDR    16    /* link-level multicast address */
  129. #define MT_MRTABLE    17    /* multicast routing tables */
  130. #if    NeXT
  131. #define    MT_MAX        32
  132. #endif    NeXT
  133.  
  134. /*
  135.  * Values for m_cltype: applicable only for cluster mbufs
  136.  */
  137. #define    MCL_STATIC    1    /* data in mbuf cluster pool */
  138. #define    MCL_LOANED    2    /* data allocated elsewhere and "loaned"
  139.                    to the mbuf */
  140.  
  141. /* flags to m_get */
  142. #define    M_DONTWAIT    0
  143. #define    M_WAIT        1
  144.  
  145. /* flags to m_pgalloc */
  146. #define    MPG_MBUFS    0        /* put new mbufs on free list */
  147. #define    MPG_CLUSTERS    1        /* put new clusters on free list */
  148. #define    MPG_SPACE    2        /* don't free; caller wants space */
  149.  
  150. /* length to m_copy to copy all */
  151. #define    M_COPYALL    1000000000
  152.  
  153. /*
  154.  * m_pullup will pull up additional length if convenient;
  155.  * should be enough to hold headers of second-level and higher protocols. 
  156.  */
  157. #define    MPULL_EXTRA    32
  158.  
  159. /*
  160.  * Macros for mbuf manipulation.
  161.  *
  162.  * Standard 4.3 defines some additional macros not given here
  163.  * (MTOCL, MCLFREE), as they are useful only within uipc_mbuf.c
  164.  * or can't cope with the existence of the MCL_LOANED cluster
  165.  * mbuf type.
  166.  *
  167.  * MCLALLOC allocates MCL_STATIC clusters.  Note that it works
  168.  * only with a count of 1 at the moment.  MCLGET connects such clusters
  169.  * to a normal mbuf.  m->m_len is set to CLBYTES upon success.
  170.  */
  171. #define    MGET(m, i, t) \
  172.     { int ms = splimp(); \
  173.       if ((m)=mfree) \
  174.         { if ((m)->m_type != MT_FREE) panic("mget"); (m)->m_type = t; \
  175.           mbstat.m_mtypes[MT_FREE]--; mbstat.m_mtypes[t]++; \
  176.           mfree = (m)->m_next; (m)->m_next = 0; \
  177.           (m)->m_off = MMINOFF; } \
  178.       else \
  179.         (m) = m_more(i, t); \
  180.       splx(ms); }
  181. /*
  182.  * Mbuf page cluster macros.
  183.  * MCLALLOC allocates mbuf page clusters.
  184.  * Note that it works only with a count of 1 at the moment.
  185.  * MCLGET adds such clusters to a normal mbuf.
  186.  * m->m_len is set to MCLBYTES upon success, and to MLEN on failure.
  187.  * MCLFREE frees clusters allocated by MCLALLOC.
  188.  */
  189. #define    MCLALLOC(m, i) \
  190.     { int ms = splimp(); \
  191.       if (mclfree == 0) \
  192.         (void)m_clalloc((i), MPG_CLUSTERS, M_DONTWAIT); \
  193.       if ((m)=mclfree) \
  194.          {++mclrefcnt[mtocl(m)];mbstat.m_clfree--;mclfree = (m)->m_next;} \
  195.       splx(ms); }
  196. #define    M_HASCL(m)    ((m)->m_off >= MSIZE)
  197. #define    MTOCL(m)    ((struct mbuf *)(mtod((m), int) &~ MCLOFSET))
  198.  
  199. #define    MCLGET(m) \
  200.     { struct mbuf *p; \
  201.       MCLALLOC(p, 1); \
  202.       if (p) { \
  203.         (m)->m_off = (int)p - (int)(m); \
  204.         (m)->m_len = MCLBYTES; \
  205.         (m)->m_cltype = MCL_STATIC; \
  206.       } else \
  207.         (m)->m_len = MLEN; \
  208.     }
  209. #define    MCLFREE(m) { \
  210.     if (--mclrefcnt[mtocl(m)] == 0) \
  211.         { (m)->m_next = mclfree;mclfree = (m);mbstat.m_clfree++;} \
  212.     }
  213. #define    MFREE(m, n) \
  214.     { int ms = splimp(); \
  215.       if ((m)->m_type == MT_FREE) panic("mfree"); \
  216.       mbstat.m_mtypes[(m)->m_type]--; mbstat.m_mtypes[MT_FREE]++; \
  217.       (m)->m_type = MT_FREE; \
  218.       if (M_HASCL(m)) { \
  219.           mclput(m); \
  220.       } \
  221.       (n) = (m)->m_next; (m)->m_next = mfree; \
  222.       (m)->m_off = 0; (m)->m_act = 0; mfree = (m); \
  223.       splx(ms); \
  224.       if (m_want) { \
  225.           m_want = 0; \
  226.           wakeup((caddr_t)&mfree); \
  227.       } \
  228.     }
  229.  
  230. /*
  231.  * Mbuf statistics.
  232.  */
  233. struct mbstat {
  234.     u_long    m_mbufs;    /* mbufs obtained from page pool */
  235.     u_long    m_clusters;    /* clusters obtained from page pool */
  236.     u_long    m_space;    /* interface pages obtained from page pool */
  237.     u_long    m_clfree;    /* free clusters */
  238.     u_long    m_drops;    /* times failed to find space */
  239.     u_long    m_wait;        /* times waited for space */
  240.     u_long    m_drain;    /* times drained protocols for space */
  241. #if    NeXT
  242.     u_short    m_mtypes[MT_MAX];    /* type specific mbuf allocations */
  243. #else    NeXT
  244.     u_short    m_mtypes[256];    /* type specific mbuf allocations */
  245. #endif    NeXT
  246. };
  247.  
  248. #ifdef    KERNEL
  249. struct mbuf *mbutl, embutl;    /* virtual address of net free mem */
  250. struct    mbstat mbstat;
  251. int    nmbclusters;
  252. struct    mbuf *mfree, *mclfree;
  253. char    mclrefcnt[NMBCLUSTERS + 1];
  254. int    m_want;
  255. struct    mbuf *m_get(),*m_getclr(),*m_free(),*m_more(),*m_copy(),*m_pullup();
  256. struct    mbuf *mclgetx();
  257. caddr_t    m_clalloc();
  258. #endif
  259.